using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Nb.FactoryAssistant.DAL.Abstracts;
using Nb.FactoryAssistant.DAL;
using Nb.FactoryAssistant.DAL.Abstracts.Entities;
using Nb.FactoryAssistant.Service.Abstracts;
using Nb.FactoryAssistant.Service.Abstracts.Dto;
using TianShu;
using Volo.Abp.Domain;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Shared;

namespace Nb.FactoryAssistant.Service
{
    /// <summary>
    /// 仓库服务实现类
    /// </summary>
    public class WmsWarehouseServiceImpl : CrudAppService<WmsWarehouse, WmsWarehouseListOutput, Guid, WmsWarehouseCreateUpdateInput>, IWmsWarehouseService
    {
        //private readonly IBattchNumberRuleDal _battchNumberRuleDal;
        private readonly IRepository<WmsWarehouseType, Guid> _warehouseTypeRepository;
        private readonly IRepository<WmsWarehouse, Guid> _warehouseRepository;
        /// <sumary>
        /// 仓库构造函数
        /// </sumary>
        public WmsWarehouseServiceImpl(IRepository<WmsWarehouseType, Guid> warehouseTypeRepository
                                        , IRepository<WmsWarehouse, Guid> warehouseRepository) : base(warehouseRepository)
        {
            _warehouseTypeRepository = warehouseTypeRepository;
            _warehouseRepository = warehouseRepository;
        }


        public override async Task<ResultDto<WmsWarehouseListOutput>> CreateAsync(WmsWarehouseCreateUpdateInput dto)
        {
            var insertEntity = MapToEntity(dto);

            insertEntity.QRCode = "";// await _battchNumberRuleDal.GetNextNumberAsync(BillOrderConsts.仓库二维码常量, insertEntity.Id);

            var codeEntity = await Repository.FindAsync(e => e.Code == insertEntity.Code);
            if (codeEntity != null)
            {
                return new ResultDto<WmsWarehouseListOutput>($"仓库code重复");
            }

            insertEntity = await Repository.InsertAsync(insertEntity);
            var data = MapToGetListOutputDto(insertEntity);
            return new  ResultDto<WmsWarehouseListOutput>(data);
        }

        public override async Task<ResultDto<WmsWarehouseListOutput>> UpdateAsync(Guid id, WmsWarehouseCreateUpdateInput dto)
        {
            var dbUpdateEntity = await Repository.FindAsync(id);
            if (dbUpdateEntity == null)
            {
                return new ResultDto<WmsWarehouseListOutput>($"The '{id}' is not found.");
            }

            if (dbUpdateEntity.Code != dto.Code)
            {
                var codeEntity = await Repository.FindAsync(e => e.Code == dto.Code);
                if (codeEntity != null)
                {
                    return new ResultDto<WmsWarehouseListOutput>($"仓库code重复");
                }
            }

            ObjectMapper.Map(dto, dbUpdateEntity);
            dbUpdateEntity = await Repository.UpdateAsync(dbUpdateEntity);
            var data = ObjectMapper.Map<WmsWarehouse, WmsWarehouseListOutput>(dbUpdateEntity);

            return new ResultDto<WmsWarehouseListOutput>(data);
        }


        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public override async Task<ResultDto<bool>> DeleteAsync(IEnumerable<Guid> ids)
        {
            return await base.DeleteAsync(ids);
        }
    }
}
